﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace MedicWave.Visualization3d
{
    /// <summary>
    /// A class describing a smooth color gradient.
    /// </summary>
    class GradientControl : ICloneable
    {
        List<Color> colors = new List<Color>();

        public List<Color> Colors
        {
            get { return colors; }
        }

        public void AddColor(Color newColor)
        {
            colors.Add(newColor);
        }


        public Color GetColor(float fScale)
        {
            if (fScale < 0) fScale = 0;
            if (fScale > 1) fScale = 1;
            if (colors.Count == 0) return Color.Black;
            if (colors.Count == 1) return colors[0];
            float step = 1.0f / (colors.Count - 1);
            for (int i = 0; i < colors.Count - 1; i++)
            {
                if (fScale >= i * step && fScale <= i * step + step)
                {
                    float factor = 1 - (fScale - i * step) / step;
                    return Color.FromArgb(
                        (int)(colors[i].A * factor + (colors[i + 1].A * (1 - factor)))
                        , (int)(colors[i].R * factor + (colors[i + 1].R * (1 - factor))),
                        (int)(colors[i].G * factor + (colors[i + 1].G * (1 - factor))),
                        (int)(colors[i].B * factor + (colors[i + 1].B * (1 - factor))));
                }
            }
            return Color.Black;
        }

        #region ICloneable Members

        public object Clone()
        {
            GradientControl newGradient = new GradientControl();
            newGradient.colors = new List<Color>(colors);
            return newGradient;
        }

        #endregion
    }
}
